package com.siruier.boss.ui.activity.commom

import android.view.View
import com.siruier.boss.api.NoticeBean
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.loginApi
import com.siruier.boss.databinding.ActivityBaseRefreshBinding
import com.siruier.boss.databinding.ItemMyMessageBinding
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.base.BaseRecyclerAdapter
import com.siruier.boss.ui.ext.startActivityExt
import com.siruier.boss.ui.helper.RefreshHelper

class NoticeActivity : BaseActivity<ActivityBaseRefreshBinding>() {

    private lateinit var mRefreshHelper: RefreshHelper<NoticeBean>
    private val mNoticeAdapter = NoticeAdapter()

    override fun initView() {
        vb.titleView.titleStr = "通知"
        mRefreshHelper = RefreshHelper(vb.refreshLayout, vb.recyclerView, mNoticeAdapter) { onReload() }
        mRefreshHelper.onRefresh()
    }

    private fun onReload() {
        launchUI({ loginApi.noticeList(mRefreshHelper.page).toData()?.records }, mRefreshHelper.handApiObserver())
    }

    class NoticeAdapter : BaseRecyclerAdapter<ItemMyMessageBinding, NoticeBean>() {
        override fun onBindViewHolder(vb: ItemMyMessageBinding, bean: NoticeBean, position: Int) {
            vb.run {
                bean.run {
                    tvTitle.text = name
                    txMsgDate.text = addTime
                    tvContent.text = content
                    tvIsRead.visibility = View.GONE
                    setViewClick(position, root)
                }
            }
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            v.context.startActivityExt<NoticeDetailsActivity> {
                putExtra("notice", getItem(position))
            }
        }
    }

}
